﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Media;
using Calendar.Properties;

namespace Calendar.Domain
{
    //public delegate void UpdateHandler();
    public class ThirtyTimer
    {
        //public UpdateHandler Update = null;
        public List<WorkStep> _workStepList { get; set; }
        public int maxSteps { get; set; }
        Timer _timer;
        SoundPlayer soundPlayer;

        //public string ThirtyTimerProgressText
        //{
        //    get
        //    {
        //        return "step." + i + " 0:" + counter;
        //    }
        //}

        public ThirtyTimer()
        {
            maxSteps = 4;
            _workStepList = new List<WorkStep>();
            WorkStep ws = new WorkStep();
            ws.Name = string.Empty;
            ws.Time = 1;
            for(int i = 0; i< maxSteps; i++)
            {
                _workStepList.Add(ws);
            }

            _timer = new Timer(1000);
            _timer.Elapsed += TickTimer;
            soundPlayer = new SoundPlayer(Resources.thirty);
        }

        int i = 0;
        int counter = 0;
        public void TickTimer(object source, ElapsedEventArgs e)//秒
        {
                
                if (counter > (_workStepList[i].Time * 60) - 1)//秒
                {
                    soundPlayer.Play();
                    i++;
                    counter = 0;
                }
                else if (i < maxSteps)
                {
                    counter++;
                }
                else
                {
                    _timer.Stop();
                }

                if (counter == 60)
                    Console.WriteLine("\n");
                else
                    Console.Write(" " + counter);
        }

        public void StartThirtyTimer()
        {
            _timer.Start();
        }

        public void SetStepsCount(int temp)
        {
            maxSteps = temp;
        }

        public string[] GetStepCount()
        {
            string[] array = new string[maxSteps];
            array.Initialize();
            for (int i = 0; i < array.Length; i++)
            {
                array.SetValue("第" + (i+1) + "步",i);
            }
            return array;
        }

        public void SetWorkStep(int id, string name, int time)//儲存
        {
            _workStepList.Add(new WorkStep());
            _workStepList[id] = new WorkStep(id, name, time);

            //Console.WriteLine("==========now List==========");
            //for (int i = 0; i < maxSteps; i++)
            //{
            //    Console.WriteLine("count: " + _workStepList.Count);
            //    Console.Write("id: " + _workStepList[i].Id);
            //    Console.Write(" name: " + _workStepList[i].Name);
            //    Console.Write(" time: " + _workStepList[i].Time);
            //    Console.WriteLine("\n");
            //}
        }

        //public bool IsExist(int id)
        //{
        //    foreach (WorkStep ws in _workStepList)
        //    {
        //        if (ws.Id == id)//存在此id
        //        {
        //            return true;
        //        }
        //        else
        //        {
        //            continue;
        //        }
        //    }
        //    return false;
        //}

        public string[] ShowWorkStepList()
        {
            string[] workStep = new string[maxSteps];
            //workStep.Initialize();
            for (int i = 0; i < maxSteps; i++)
                workStep[i] = "step" + (i+1) + ". " + _workStepList[i].Name + "     " + _workStepList[i].Time + "分鐘";
               
            return workStep;
        }

        public string GetName(int index)
        {
            //Console.WriteLine("index >> "+ index + "..." +_workStepList[index].Name);
            //if (_workStepList[index].Name != string.Empty)
            //{
            //    //Console.WriteLine("not empty");
            //Console.WriteLine(_workStepList[index].Name);
            //Console.WriteLine(index);
            return _workStepList[index].Name;
                
            //}
            //else
            //{
            //    //Console.WriteLine("empty");
            //    return " ";
            //}
        }

        public int GetTime(int index)
        {
            //Console.WriteLine(_workStepList[index].Time);
            return _workStepList[index].Time;
        }
    }
}
